import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/device_data_controller.dart';
import '../views/device_data_view.dart';
// 导出MultiSeriesChartPainter类
export '../views/device_data_view.dart' show MultiSeriesChartPainter; 

class FullscreenChartView extends StatelessWidget {
  final List<ChartSeries> seriesList;
  final ThemeData theme;
  final double minY;
  final double maxY;
  final int totalPoints;

  const FullscreenChartView({
    Key? key,
    required this.seriesList,
    required this.theme,
    required this.minY,
    required this.maxY,
    required this.totalPoints,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Get.back(),
        ),
        title: const Text('全屏图表'),
        centerTitle: true,
      ),
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: _buildFullscreenChart(),
            ),
            Container(
              height: 30,
              alignment: Alignment.center,
              child: Text(
                '双指缩放或拖动图表可查看更多细节',
                style: TextStyle(fontSize: 10, color: Colors.grey.shade600),
                textAlign: TextAlign.center,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildFullscreenChart() {
    return FullscreenZoomableChart(
      seriesList: seriesList,
      theme: theme,
      minY: minY,
      maxY: maxY,
      totalPoints: totalPoints,
    );
  }
}

// 全屏缩放图表组件
class FullscreenZoomableChart extends StatefulWidget {
  final List<ChartSeries> seriesList;
  final ThemeData theme;
  final double minY;
  final double maxY;
  final int totalPoints;

  const FullscreenZoomableChart({
    Key? key,
    required this.seriesList,
    required this.theme,
    required this.minY,
    required this.maxY,
    required this.totalPoints,
  }) : super(key: key);

  @override
  _FullscreenZoomableChartState createState() => _FullscreenZoomableChartState();
}

class _FullscreenZoomableChartState extends State<FullscreenZoomableChart> {
  double _scale = 1.0;
  double _offsetX = 0.0;
  double? _startScale;
  double? _startOffsetX;
  Offset? _lastFocalPoint;

  @override
  Widget build(BuildContext context) {
    final visibleSeries = widget.seriesList.where((s) => s.isVisible.value).toList();
    
    if (visibleSeries.isEmpty) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              Icons.visibility_off,
              size: 32,
              color: widget.theme.disabledColor,
            ),
            const SizedBox(height: 12),
            Text(
              '请选择至少一个数据系列显示',
              style: TextStyle(color: widget.theme.disabledColor),
            ),
          ],
        ),
      );
    }

    return GestureDetector(
      onScaleStart: (details) {
        _startScale = _scale;
        _startOffsetX = _offsetX;
        _lastFocalPoint = details.focalPoint;
      },
      onScaleUpdate: (details) {
        setState(() {
          if (_startScale != null) {
            _scale = (_startScale! * details.scale).clamp(1.0, 10.0);
          }
          
          if (_lastFocalPoint != null && details.scale == 1.0) {
            final dx = details.focalPoint.dx - _lastFocalPoint!.dx;
            final availableDx = MediaQuery.of(context).size.width;
            
            if (availableDx > 0) {
              final newOffsetX = _offsetX - (dx / availableDx);
              _offsetX = newOffsetX.clamp(0.0, 1.0);
            }
          }
          
          _lastFocalPoint = details.focalPoint;
        });
      },
      onScaleEnd: (details) {
        _startScale = null;
        _startOffsetX = null;
        _lastFocalPoint = null;
      },
      child: Stack(
        children: [
          CustomPaint(
            size: Size.infinite,
            painter: MultiSeriesChartPainter(
              seriesList: visibleSeries,
              minY: widget.minY,
              maxY: widget.maxY,
              scale: _scale,
              offsetX: _offsetX,
              maxPointsPerSeries: widget.totalPoints > 1000 ? 100 : 200,
            ),
          ),
          Positioned(
            top: 8,
            right: 8,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
              decoration: BoxDecoration(
                color: Colors.black.withOpacity(0.6),
                borderRadius: BorderRadius.circular(4),
              ),
              child: Text(
                '缩放倍数: ${_scale.toStringAsFixed(1)}x',
                style: const TextStyle(color: Colors.white, fontSize: 10),
              ),
            ),
          ),
        ],
      ),
    );
  }
}